﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Collections;

namespace WEALTH.Manager
{
    public partial class Welcome : BasePage
    {
        ExcuteDao ExcuteDao = new ExcuteDao();
        ExcuteDao user = new ExcuteDao();
        private string UserID = null;
        public StringBuilder str = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                UserID = Session["UserID"].ToString();
            }

            if (!IsPostBack)
            {
                str = new StringBuilder();
                UserMenuTreeShow("1", "后台管理");

            }

            if (Session["msg"] != null)
            {
                ShowMsg(Session["msg"].ToString(), 0);
                Session["msg"] = null;
            }
        }
        public void UserMenuTreeShow(string menuCode, string menuName)
        {
            string url = "";
            ViewState["ds"] = ExcuteDao.GetQueryStrs(Session["UserID"].ToString());
            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);
            dvTree.RowFilter = " Menu_Pcode = '" + menuCode + "'";


            TreeNode Node;
            foreach (DataRowView Row in dvTree)
            {
                ArrayList list = ia();
                if (list.Contains(Row["Menu_Code"].ToString()))
                {
                    Node = new TreeNode();
                    url = Row["Menu_FileName"].ToString().Trim();
                    str.Append("<a onclick='change(this);' style='color:black; text-decoration:none;' target='content' href='" + url + "'><div class='menu-tit'>" + Row["Menu_Name"].ToString() + "</div></a>");

                    int FileUrl = Row["Menu_FileName"].ToString().Trim() == "Default.aspx" ? 0 : 1;

                    if (FileUrl == 0)
                    {
                        str.Append("<div class='menu-list'>");
                    }
                    else
                    {
                        str.Append("<div class='menu-list'> <div class='top-line'></div> <ul class='nav-items'>");
                    }


                    AddTree(Row["Menu_Code"].ToString());    //再次递归
                    str.Append("</ul> </div>");
                }
            }
        }


        public ArrayList ia()
        {
            DataTable dtvalue = ExcuteDao.GetAllValidUser(UserID);
            ArrayList myArrList = new ArrayList();
            myArrList.Clear();
            for (int i = 0; i < dtvalue.Rows.Count; i++)
            {
                myArrList.Add(dtvalue.Rows[i]["FK_Menu_Code"].ToString());
            }
            return myArrList;
        }

        public void AddTree(String ParentID)
        {
            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);
            dvTree.RowFilter = " Menu_Pcode = '" + ParentID + "'";
            string url = "";
            foreach (DataRowView Row in dvTree)
            {
                ArrayList list = ia();

                if (list.Contains(Row["Menu_Code"].ToString()))
                {
                    //if (Row["Menu_FileName"].ToString().Trim() != "")
                    //{
                    url = Row["Menu_FileName"].ToString().Trim();

                    str.Append("<li><a onclick='change(this);' class = 'selectOffstyle' href=" + url + " target='content'>" + Row["Menu_Name"].ToString() + "</a></li>");
                    //}

                    AddTree(Row["Menu_Code"].ToString());    //再次递归
                }
            }
        }
   
    }
}